﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace G.Controls
{
    /// <summary>
    /// 可编辑控件，拥有编辑和正常两种状态，使用的时候请不要滥用，只有将真正需要被编辑的部分作为可编辑控件
    /// </summary>
    [TemplateVisualState(GroupName = "EditeState", Name = "Normal")]
    [TemplateVisualState(GroupName = "EditeState", Name = "Edite")]
    public class EditeControl : ContentControl
    {
        public static readonly DependencyProperty IsEditeProperty = DependencyProperty.Register("IsEdite", typeof(bool), typeof(EditeControl), new PropertyMetadata(false, (e, s) =>
        {
            EditeControl edite = e as EditeControl;
            if (edite.IsEdite)
                VisualStateManager.GoToState(edite, "Edite", false);
            else
                VisualStateManager.GoToState(edite, "Normal", false);
        }));
        public bool IsEdite
        {
            get
            {
                return (bool)this.GetValue(EditeControl.IsEditeProperty);
            }
            set
            {
                this.SetValue(EditeControl.IsEditeProperty, value);
            }
        }

        public static readonly DependencyProperty EditeContentProperty = DependencyProperty.Register("EditeContent", typeof(object), typeof(EditeControl), null);
        public object EditeContent
        {
            get { return this.GetValue(EditeControl.EditeContentProperty); }
            set { this.SetValue(EditeControl.EditeContentProperty, value); }
        }

        public static readonly DependencyProperty EditeContentTemplateProperty = DependencyProperty.Register("EditeContentTemplate", typeof(DataTemplate), typeof(EditeControl), null);

        public DataTemplate EditeContentTemplate
        {
            get { return (DataTemplate)this.GetValue(EditeControl.EditeContentTemplateProperty); }
            set { this.SetValue(EditeControl.EditeContentTemplateProperty, value); }
        }

        public EditeControl()
        {
            this.DefaultStyleKey = typeof(EditeControl);
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            if (this.IsEdite)
                VisualStateManager.GoToState(this, "Edite", false);
            else
                VisualStateManager.GoToState(this, "Normal", false);
        }
    }
}
